<template>
  <div class="app-container">
    <div class="fly-card-head">
      <div class="fly-card-head-wrapper">
        <div class="fly-card-head-title">
          <div class="head-title">发件服务器设置</div>
        </div>
      </div>
    </div>
    <div class="fly-card-body">
      <el-form ref="form" :model="form" :rules="rules" label-width="180px">
        <el-form-item label="发件服务器" prop="mailSmtpServer">
          <el-input v-model="form.mailSmtpServer" placeholder="请输入发件服务器" />
        </el-form-item>
        <el-form-item label="发件人昵称" prop="mailUserName">
          <el-input v-model="form.mailUserName" placeholder="请输入发件人昵称" />
        </el-form-item>
        <el-form-item label="发件箱账号" prop="mailSmtpUsermail">
          <el-input type="email" auto-complete="off" v-model="form.mailSmtpUsermail" placeholder="请输入发件箱账号" />
        </el-form-item>
        <el-form-item label="发件箱密码" prop="mailSmtpPassword">
          <el-input type="password" auto-complete="off" v-model="form.mailSmtpPassword" placeholder="请输入发件箱密码" />
        </el-form-item>
        <el-form-item label="发件箱端口" prop="mailSmtpPort">
          <el-input v-model="form.mailSmtpPort" placeholder="请输入发件箱端口" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-input type="hidden" v-model="form.mailServer" />
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
  </div>
  </div>
</template>

<script>
import { getEmail,  updateEmail } from "@/api/notify/email";

export default {
  name: "Email",
  data() {
    return {
      // 表单参数
      form: {
        passw: "password",
      },
      // 表单校验
      rules: {
        mailSmtpServer: [
          { required: true, message: "发件服务器不能为空", trigger: "blur" }
        ],
        mailUserName: [
          { required: true, message: "发件人昵称不能为空", trigger: "blur" }
        ],
        mailSmtpUsermail: [
          { required: true, message: "发件箱账号不能为空", trigger: "blur" }
        ],
        mailSmtpPassword: [
          { required: true, message: "发件箱密码不能为空", trigger: "blur" }
        ],
        mailSmtpPort: [
          { required: true, message: "发件箱端口不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getEmail();
  },
  methods: {
    /** 查询邮件服务器 */
    getEmail() {
		this.reset();
		const key = "flymail";
		getEmail(key).then(response => {
		  this.form = response.data;
		});
    },

    // 取消按钮
    cancel() {
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        mailSmtpServer: undefined,
        mailUserName: undefined,
        mailSmtpUsermail: undefined,
        mailSmtpPassword: undefined,
        mailSmtpPort: undefined
      };
      this.resetForm("form");
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.reset();
    },

    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateEmail(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess("修改成功");
            } else {
              this.msgError(response.msg);
            }
          });
        }
      });
    }
  }
};
</script>
